<?php

declare(strict_types=1);

namespace app\admin\command;

use app\common\model\Matches;
use app\common\model\MatchesAnalysis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Analysis extends Command
{
    protected function configure()
    {
        // 指令配置
        //根据每天的赛事爬取后执行此定时器。和 文字直播及阵容指数一致
        $this->setName('analysis')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->setDescription('赛事统计数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = trim($input->getArgument('action'));
        $start = strtotime(date('Y-m-d', time()) . '00:00:00');
        $end = strtotime(date('Y-m-d', time()) . '23:59:59');
        Matches::field('id,api_id,match_id,home_team_name as name,type')->where(['type' => $action])->whereTime('match_time', 'between', [$start, $end])->chunk(100, function ($matches) use ($action, $output) {
            foreach ($matches as $key => $matche) {
                $output->writeln('=========================循环第' . ($key + 1) . '个数据，开始循环赛事名称为:' . $matche['name'] . '=====================================');
                // 处理结果集...
                if ($action == 'football') {
                    $match_id = $matche['match_id'];
                } else {
                    $match_id = $matche['api_id'];
                }
                $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/match/analysis?match_id=' . $match_id;
                $res = curl_get($url);
                $arr = json_decode($res, true);
                if ($arr['code'] == 0 && count($arr['data']) > 0) {
                    if (array_key_exists('vs', $arr['data']['history']) && !empty($arr['data']['history']['vs'])) {
                        $history_vs = json_encode($arr['data']['history']['vs'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $history_vs = NULL;
                    }
                    if (array_key_exists('home', $arr['data']['history']) && !empty($arr['data']['history']['home'])) {
                        $history_home = json_encode($arr['data']['history']['home'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $history_home = NULL;
                    }
                    if (array_key_exists('away', $arr['data']['history']) && !empty($arr['data']['history']['away'])) {
                        $history_away = json_encode($arr['data']['history']['away'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $history_away = NULL;
                    }

                    if (array_key_exists('goal_distribution', $arr['data']) && !empty($arr['data']['goal_distribution'])) {
                        $goal_distribution = json_encode($arr['data']['goal_distribution'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $goal_distribution = NULL;
                    }
                    if (array_key_exists('fixture', $arr['data']) && !empty($arr['data']['fixture'])) {
                        $fixture = json_encode($arr['data']['fixture'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $fixture = NULL;
                    }
                    if (array_key_exists('table', $arr['data']) && !empty($arr['data']['table'])) {
                        $table = json_encode($arr['data']['table'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $table = NULL;
                    }
                    $result_arr = [
                        'history_vs' => $history_vs,
                        'history_home' => $history_home,
                        'history_away' => $history_away,
                        'goal_distribution' => $goal_distribution,
                        'fixture' => $fixture,
                        'table' => $table,
                        'match_id' => $match_id,
                        'foreign_key_id' => $matche->id,
                        'type' => $action
                    ];
                    $is_find = MatchesAnalysis::where(['foreign_key_id' => $matche->id])->find();
                    if (empty($is_find)) {
                        MatchesAnalysis::create($result_arr);
                        $str = 'add';
                    } else {
                        //更新
                        $is_find->save($result_arr);
                        $str = 'update';
                    }
                    $output->writeln('=========================' . $str . '完成=====================================');
                } else {
                    $output->writeln('api参数出错');
                }
            }
        });
    }
}
